package com.geostar.mina.encoder;

import java.nio.charset.Charset;  
import java.nio.charset.CharsetEncoder;  
import org.apache.mina.core.buffer.IoBuffer;  
import org.apache.mina.core.session.IoSession;  
import org.apache.mina.filter.codec.ProtocolEncoder;  
import org.apache.mina.filter.codec.ProtocolEncoderOutput;  

import com.geostar.mina.model.ProcessModel;
  
public class HEncoder implements ProtocolEncoder{  
    private final Charset charset;  
  
    public HEncoder(Charset charset) {  
        this.charset = charset;  
    }  
  
    @Override  
    public void encode(IoSession session, Object message,  
            ProtocolEncoderOutput out) throws Exception {  
    	 CharsetEncoder ce = charset.newEncoder();  
         ProcessModel pm = (ProcessModel) message;
         IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);  
         buffer.putString(pm.getId(),ce);
         buffer.putString(pm.getIp(),ce);
         buffer.putString(pm.getProcess_name(), ce);
         buffer.putString(pm.getIsRunning(), ce);
         buffer.putString(pm.getRecord_time(), ce);
         buffer.putString(pm.getRecord_time(), ce);
         
         buffer.flip();  
         out.write(buffer);  
    }  
  
    @Override  
    public void dispose(IoSession session) throws Exception {  
        // TODO Auto-generated method stub  
          
    }  
}  